package com.company.tips.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.company.corelib.facade.UserFacade;
import com.company.corelib.model.Role;
import com.company.corelib.model.User;

@Controller
@RequestMapping("admin/user/")
public class UserController {
	private UserFacade userFacade = new UserFacade();
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public ModelAndView index() {
		System.err.println("redirect to list user");
		ModelAndView model = new ModelAndView();
		model.addObject("users",userFacade.getAllUser());
		model.setViewName("admin/user/list");
		return model;
	}
	@RequestMapping(value = "create", method = RequestMethod.POST)
	public ModelAndView create(
			@RequestParam(value="email", required=false) String email,
			@RequestParam(value="username", required=false) String username,
			@RequestParam(value="password", required=false) String password,
			@RequestParam(value="fullname", required=false) String fullname,
			@RequestParam(value="gender", required=false) String gender,
			@RequestParam(value="role", required=false) String role){
		ModelAndView model = new ModelAndView();
		model.addObject("users",userFacade.getAllUser());
		model.addObject("roles",userFacade.getAllRole());
		if(username != null){
			User user = new User();
			user.setEmail(email);
			user.setFullname(fullname);
			user.setGender(gender);
			user.setLikecount(0);
			user.setPassword(password);
			user.setStatus(1);
			user.setUsername(username);
			user.setRole(Integer.parseInt(role));
			System.err.println("user fullname from controller = " + user.getFullname());
			userFacade.create(user);
			model.setViewName("redirect:list");
		}else{
			model.setViewName("/admin/user/create");
		}
		return model;
	}
	
	@RequestMapping(value = "edit", method = RequestMethod.POST)
	public ModelAndView update(
			@RequestParam(value="userid", required=false) String userid,
			@RequestParam(value="email", required=false) String email,
			@RequestParam(value="username", required=false) String username,
			@RequestParam(value="password", required=false) String password,
			@RequestParam(value="fullname", required=false) String fullname,
			@RequestParam(value="gender", required=false) String gender,
			@RequestParam(value="status", required=false) String status,
			@RequestParam(value="role", required=false) String role){
		System.err.println("userid = " + userid);
		ModelAndView model = new ModelAndView();
		User user = userFacade.getUserById(userid);
		if(username != null){
			model.addObject("user",userFacade.getAllUser());
			user.setEmail(email);
			user.setFullname(fullname);
			user.setGender(gender);
			user.setPassword(password);
			user.setStatus(Integer.parseInt(status));
			user.setUsername(username);
			user.setRole(Integer.parseInt(role));
			userFacade.update(user);
			model.setViewName("redirect:list");
		}else{
			model.addObject("user",user);
			model.addObject("userRole",userFacade.getRoleById(user.getRole()));
			model.addObject("roles",userFacade.getAllRole());
			model.setViewName("/admin/user/edit");
		}
		return model;
	}

	@RequestMapping(value = "delete", method = RequestMethod.POST)
	public ModelAndView delete(
			@RequestParam(value="userid", required=false) String userid){
		System.err.println("userid = " + userid);
		ModelAndView model = new ModelAndView();
		userFacade.delete(userid);
		model.setViewName("redirect:list");
		return model;
	}
}
